﻿using MvcContrib;
using System;
using System.Collections.Generic;
using System.Web.Mvc;


namespace ProductInfo.Core.Controllers
{
	public class ReleaseNoteEntryController : Controller
    {
		private readonly ProductInfo.Core.Service.IReleaseNoteEntryService c_releaseNoteEntryService;


		public ReleaseNoteEntryController(
			ProductInfo.Core.Service.IReleaseNoteEntryService releaseNoteEntryService)
		{
			this.c_releaseNoteEntryService = releaseNoteEntryService;
		}


		public ActionResult List()
        {
			var _releaseNotes = this.c_releaseNoteEntryService.List();

			return View(_releaseNotes);
        }


		public ActionResult Edit(
			int Id)
		{
			var _releaseNote = this.c_releaseNoteEntryService.Edit(Id);

			return View(_releaseNote);
		}


		[HttpPost]
		public ActionResult Edit(
			ProductInfo.Core.Models.ReleaseNoteEntry releaseNoteEntry)
		{
			if (!this.c_releaseNoteEntryService.Update(releaseNoteEntry))
			{
				this.UpdateModelStateErrorCollection(this.c_releaseNoteEntryService.ValidationErrors);
				return View();
			}
			
			return this.RedirectToAction<ReleaseNoteEntryController>(controller => controller.List()); 
		}


		public ActionResult Create()
		{
			return View();
		}


		[HttpPost]
		public ActionResult Create(
			[Bind(Exclude="Id")] ProductInfo.Core.Models.ReleaseNoteEntry releaseNoteEntry)
		{
			if (! this.c_releaseNoteEntryService.Create(releaseNoteEntry))
			{
				this.UpdateModelStateErrorCollection(this.c_releaseNoteEntryService.ValidationErrors);
				return View();
			}
				
			return this.RedirectToAction<ReleaseNoteEntryController>(controller => controller.List()); 
		}


		public ActionResult UpdateVersion()
		{
			var _releaseNoteEntryVersion = new ProductInfo.Core.Models.ReleaseNoteEntryVersion
          		{
          			VersionValues = new SelectList(this.c_releaseNoteEntryService.FetchDistinctVersions())
          		};

			return View(_releaseNoteEntryVersion);
		}


		[HttpPost]
		public ActionResult UpdateVersion(
			ProductInfo.Core.Models.ReleaseNoteEntryVersion releaseNoteEntryVersion)
		{
			// If errors exist, we need to get version list from database as this will not be sent from view!
			if (!ModelState.IsValid)
			{
				var _result = new ProductInfo.Core.Models.ReleaseNoteEntryVersion
					{
						VersionValues = new SelectList(this.c_releaseNoteEntryService.FetchDistinctVersions())
					};
				_result.UpdateVersion = releaseNoteEntryVersion.UpdateVersion;

				return View(_result);
			}
		
			this.c_releaseNoteEntryService.UpdateVersion(
				releaseNoteEntryVersion.OriginalVersion,
				releaseNoteEntryVersion.UpdateVersion);

			return this.RedirectToAction<ReleaseNoteEntryController>(controller => controller.List()); 
		}


		private void UpdateModelStateErrorCollection(
			IDictionary<string, string> subject)
		{
			foreach (var item in subject) { this.ModelState.AddModelError(item.Key, item.Value); }
		}
    }
}
